package cn.dnui_yxk801.dnuifood_16110100801.model;

import android.app.Service;

import java.util.List;

import cn.dnui_yxk801.dnuifood_16110100801.bean.CollectBean;
import cn.dnui_yxk801.dnuifood_16110100801.bean.ResultBean;
import cn.dnui_yxk801.dnuifood_16110100801.iface.FoodIface;
import cn.dnui_yxk801.dnuifood_16110100801.listener.OnRetrofitListener;
import cn.dnui_yxk801.dnuifood_16110100801.service.FoodService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FoodModel extends BaseModel implements FoodIface{
    private FoodService service;
    public FoodModel()
    {
        //super();
        service=retrofit.create(FoodService.class);
    }
    @Override
    public void getAllCommentsByFood(String food_id,final OnRetrofitListener listener) {
        Call call=service.getAllCommentsByFood(food_id);
        //bindCallBack(call,listener,1);
        Callback<List> callback = new Callback<List>() {
            @Override
            public void onResponse(Call<List> call, Response<List> response) {
                listener.onSuccess(response.body(), 2);
            }

            @Override
            public void onFailure(Call<List> call, Throwable t) {
                listener.onFalure(t.toString());
            }
        };
        call.enqueue(callback);
    }

    @Override
    public void getFoodById(String food_id, OnRetrofitListener listener) {
        Call call=service.getFoodById(food_id);
        bindCallBack(call,listener,1);
    }
    public void isCollected(String userid, String food_id,String flag, final OnRetrofitListener listener) {
        Call call=service.isCollected(userid,food_id,flag);
        Callback<CollectBean> callback = new Callback<CollectBean>() {
            @Override
            public void onResponse(Call<CollectBean> call, Response<CollectBean> response) {
                listener.onSuccess(response.body(), 3);
            }

            @Override
            public void onFailure(Call<CollectBean> call, Throwable t) {
                listener.onFalure(t.toString());
            }
        };
        call.enqueue(callback);
    }
    public void insertOrder(String userid, String food_id,String num,String sum,String suggesttime,String address, final OnRetrofitListener listener) {
        Call call=service.insertOrder(userid,food_id,num,sum,suggesttime,address);
        Callback<ResultBean> callback = new Callback<ResultBean>() {
            @Override
            public void onResponse(Call<ResultBean> call, Response<ResultBean> response) {
                listener.onSuccess(response.body(), 5);
            }

            @Override
            public void onFailure(Call<ResultBean> call, Throwable t) {
                listener.onFalure(t.toString());
            }
        };
        call.enqueue(callback);
    }

    @Override
    public void getFoodBySearch(String search,OnRetrofitListener listener) {
        Call call=service.getFoodBySearch(search);
        bindCallBack(call,listener,8);
    }
}
